package cn.lyl.sys.service.impl;

import cn.lyl.sys.domain.Role;
import cn.lyl.sys.mapper.RoleMapper;
import cn.lyl.sys.service.IRoleService;
import cn.lyl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    public void add(Role role) {
        super.add(role);

        //添加当前角色的菜单
        if(role.getMenus() != null && role.getMenus().size() > 0 ){
            roleMapper.addRoleMenu(role.getId(),role.getMenus());
        }

        //添加当前角色的权限
        if (role.getPermissions() != null && role.getPermissions().size() > 0){
            roleMapper.addRolePermissions(role.getId(),role.getPermissions());
        }
    }

    @Override
    public void update(Role role) {
        //删除当前所有角色的菜单
        roleMapper.delMenuByRoleId(role.getId());
        //删除当前所有角色的权限
        roleMapper.delPermissionsByRoleId(role.getId());
        //添加当前角色的菜单
        if (role.getMenus() != null && role.getMenus().size() > 0){
            roleMapper.addRoleMenu(role.getId(),role.getMenus());
        }
        //添加当前角色的权限
        if (role.getPermissions() != null && role.getPermissions().size() > 0){
            roleMapper.addRolePermissions(role.getId(),role.getPermissions());
        }
        super.update(role);
    }

    @Override
    public void del(Long id) {
        //删除当前所有角色的菜单
        roleMapper.delMenuByRoleId(id);
        //删除当前所有角色的权限
        roleMapper.delPermissionsByRoleId(id);
        //删除当前角色和员工中间表数据
        roleMapper.delEmployeeRoleByRoleId(id);
        //删除角色
        super.del(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }
}
